<template>
  <div class="com" :class="{'size-l': size == 'large', 'size-s': size == 'small'}" @tap.stop="handlePlay">
    <i v-if="status == 'unstart'" class="if icon-sound"></i>
    <i v-if="status == 'loading'" class="if icon-refresh c-gray spin"></i>
    <i v-if="status == 'playing'" class="if icon-sound shing"></i>
    <i v-if="status == 'ended'" class="if icon-sound"></i>
  </div>
</template>

<script>
const COS_ADDR = process.env.COS_ADDR

export default {
  props: {
    path: [String],
    size: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      status: 'unstart'
    }
  },
  computed: {
    srcUrl() {
      if (!this.path) return ''
      if (this.path.indexOf('//') >= 0) return this.path
      return COS_ADDR + this.path
    },
  },
  methods: {
    handlePlay () {
      if (!['ended', 'unstart'].includes(this.status)) return
      let that = this
      this.audioCtx = wx.createInnerAudioContext()
      this.status = 'loading'
      this.audioCtx.obeyMuteSwitch = false
      this.audioCtx.src = this.srcUrl
      this.audioCtx.play()
      this.audioCtx.onPlay(() => {
        this.status = 'playing'
      })
      this.audioCtx.onEnded(() => {
        this.status = 'ended'
      })
    }
  }
}
</script>

<style scoped lang="less">
@import '../assets/less/var.less';
.com {
  display: inline-block;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  text-align: center;
  box-shadow: 0 0 5px rgba(0,0,0,.2);
  color: @colorPrimary;
  i {
    line-height: 50px;
    font-size: 1.6em;
  }
  &.size-l {
  }
}
</style>
